package com.example.demo.Controller;
import com.example.demo.Enity.privateList;
import com.example.demo.Enity.privateTalk;
import com.example.demo.dao.ChatJpa;
import com.example.demo.dao.ChatlistJpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class Chat {
@Autowired
    ChatJpa chatJpa;
@Autowired
    ChatlistJpa chatlistJpa;
//添加聊天信息
    @RequestMapping("/addPrivateTalk")
    public void addPrivateTalk(String sender,String toUser,String message){
    chatJpa.addPrivateTalk(sender, toUser, message);
    //聊天列表
        //不是自己跟自己聊天
        if(!sender.equals(toUser)) {
            int shu=chatlistJpa.getsum(sender, toUser);
            System.out.println("shu="+shu);
            if(shu == 0){
                privateList p=new privateList();
                p.setMessage(message);
                p.setMineid(sender);
                p.setOtherid(toUser);
                chatlistJpa.save(p);
                return;
            }
            chatlistJpa.changeRelate(sender, toUser, message);
            return;
        }
        //跟自己聊天
        int shu2=chatlistJpa.getsum2(sender, toUser);
        if(shu2 == 0) {
            privateList p=new privateList();
            p.setMessage(message);
            p.setMineid(sender);
            p.setOtherid(toUser);
            chatlistJpa.save(p);
            return;
        }
        System.out.println("到这边");
        chatlistJpa.changeRelate2(sender, toUser, message);
    return ;
    }
// 查找聊天信息
    @RequestMapping("/findChathistory")
    public ArrayList findChathistory(String sender,String toUser){
        ArrayList<privateTalk> a=new ArrayList<>();
        //如果不是和自己聊天
        if(!sender.equals(toUser)){
            a=chatJpa.findChathistory(sender, toUser);
            return a;
        }
        //如果是和自己聊天
            a=chatJpa.findChathistory2(sender, toUser);
            return a;
    }
    // （好友列表）查找聊过天的好友
    @RequestMapping("/findFriendsList")
    public ArrayList findFriendsList(String sender){
        ArrayList a = new ArrayList<>();
        a = chatJpa.findFriendsList(sender);
//        System.out.println(sender);
//        System.out.println(a);
        return a;
    }
    //删除聊天；列表
    @RequestMapping("/deleteCharlist")
    public void deleteChatlist(String sender,String toUser){

        System.out.println(sender+toUser);
        chatlistJpa.deleteChatlist(sender, toUser);
    }
}
